/**
 * 
 */
package edu.gatech.volt2.drdoommgt.domain;

import java.util.Calendar;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Query;
import javax.persistence.Table;
import javax.persistence.Transient;

import edu.gatech.volt2.drdoommgt.system.DataBaseAccessor;
import edu.gatech.volt2.drdoommgt.system.DataStore;

/**
 * @author James Rundquist
 *
 */

@Entity
@Table(name="nurses")
public class Nurse extends User {

	@Transient
	private List<Appointment> appointments; 
		
	/**
	 * Default constructor
	 */
	public Nurse() {
		// TODO Auto-generated constructor stub
		super();
		this.setType(UserType.NURSE);
	}

	/**
	 * Constructor to find and populate this nurse information from the data storage
	 * @param id id of the nurse
	 */
	public Nurse(int id) {
		super(id);
		// :TODO: Get nurse information from the database
		this.setType(UserType.NURSE);
	}
	
	/**
	 * This constructor creates a nurse object with a name
	 * 
  	 * @param name the name of the nurse to be created
	 */
	public Nurse(Name name){
		super(name);
		this.setType(UserType.NURSE);
	}
	
	/**
	 * Sets the appointment list 
	 * @param appointments the appointments to set
	 */
	public void setAppointments(List<Appointment> appointments) {
		this.appointments = appointments;
	}

	/**
	 * Return the appointments
	 * @return the list of appointments
	 */
	public List<Appointment> getAppointments() {
		return appointments;
	}

	
	/**
	 * Creates a new patient with the passed information and saves it in the data storage 
	 * @param name Name of the patient
	 * @param info Patient Info for the new patient
	 * @return boolean whether the patient was successfully created
	 */
	public boolean createPatient(Name name, PatientInfo info){
		@SuppressWarnings("unused")
		Patient newPatient = new Patient(name, info);
		//return DataAccessLayer.save(newPatient);
		return true;
	}
	
	/**
	 * This method deletes a patient
	 * 
	 * @param id the id number of the patient
	 */
	public boolean deletePatient(int id){
		// Deletes patient of id id
		return true|false;
	}
	
	
	/// I think this should go to the presenter and just be a permission granted to Nurse
	public void viewPatients(){
		// select all patients from the database
		// pass to the patients to the presenter
	}
	
	public boolean editPatientDetails(int id){
		return false;/// Same with this method as with viewPatientes
	} 
	
	/**
	 * This method creates a treatment record for a patient
	 * 
	 * @param p the patient of the treatment record to be created
	 */
	public boolean createTreatmentRecord(Patient p){
		return true;
	}
	
	/**
	 * This method views the appointments for a specified date
	 * 
	 * @param today the date of the appointments
	 */
	public void viewDailyAppointments(Calendar today){
		// Select all appointments
		// Pass them all to the presenter
	}
	
	/**
	 * This method confirms an appointment
	 * 
	 * @param appointment the appointment to be confirmed
	 */
	public void confirmAppointment(Appointment appointment){
		appointment.setConfirmed(true);
		DataBaseAccessor db = new DataBaseAccessor();
		db.connectToDatabase();
		db.save(appointment);
		db.closeConnectionToDatabase();
	}

	/**
	 * This method searches for a patient by his or her name
	 * 
	 * @param name the name of the patient to be searched for
	 * @return the patient
	 */
	public Patient lookUpPatient(Name name)
	{
		List<Patient> patientFoundList = Patient.findByName(name);
		return patientFoundList.get(0);
	}
	
	/**
	 * This method creates an invoice for a patient
	 * 
	 * @param patient the patient that the invoice is to be created for
	 */
	public Invoice createInvoice(Patient patient)
	{	
		String name= patient.getName().toString();
		PatientInfo info= patient.getInfo();
		List<TreatmentRecord> treatments= info.getMedHistory();
		TreatmentRecord lastVisit= null;
		Doctor doc= null;
		Calendar date= null;
		BillingInfo billInfo= null;
		if(treatments.size()>0)
		{
			lastVisit= treatments.get(treatments.size()-1);
		}
		else
		{
			System.out.println("There is not a medical history for this patient.");
		}
		
		if(lastVisit != null)
		{
			doc= lastVisit.getTreatingDoc();
			date= lastVisit.getDate();
			billInfo= new BillingInfo(lastVisit);
			
		}
		
		Invoice invoice= new Invoice(patient, billInfo, date, doc);
		  
		return invoice;
		
	}
	
	/**
	 * This method allows the nurse to find a user by his or her email address
	 * 
	 * @param email the email address to be searched
	 */
	@SuppressWarnings("unchecked")
	public static List<Nurse> findByEmail(String email){
		Query findByEmailQuery=null;
		findByEmailQuery = DataStore.getEm().createQuery("from Nurse where email = :email");
		findByEmailQuery.setParameter("email", email);
		List<Nurse> returnList = null;
		returnList = findByEmailQuery.getResultList();
		return returnList;
	}
	
	
	public String toString(){
		return this.getName().toString();
	}
	
}
